package org.yray.graphql;

import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
import static graphql.Scalars.*;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;
public class GraphQLDemo {
    /**
     * graphql-java 提供了两种不同的方式来定义模式：
     * 以编程方式作为Java代码或通过特殊的graphql dsl（称为SDL）
     * @param args
     */
    public static void main(String[] args) {
        GraphQLObjectType userObjectType = createUserObjectType();
        GraphQLFieldDefinition userDefinition =
                createUserDefinition(userObjectType);
        GraphQLFieldDefinition cardDefinition =createCardTypeDefinition(createCardObjectType());
        GraphQL graphQL =
                GraphQL.newGraphQL(createGraphqlSchema(userDefinition,cardDefinition)).build();
        String query = "{user{id},card{cardNumber}}";
        ExecutionResult executionResult = graphQL.execute(query);
        // 打印错误
        System.out.println("错误：" + executionResult.getErrors());
        // 打印数据
        System.out.println("结果：" +(Object) executionResult.getData());
    }
    /**
     * 定义Schema
     * <p>
     * schema { #定义查询
     *   query: UserQuery
     * }
     *
     * @return
     */
    public static GraphQLSchema createGraphqlSchema(GraphQLFieldDefinition
                                                            userDefinition,GraphQLFieldDefinition card) {
        GraphQLObjectType userQuery = newObject().name("userQuery")
                .field(userDefinition).field(card).build();
        return GraphQLSchema.newSchema().query(userQuery).build();
    }
    /**
     * 定义查询的类型
     * <p>
     * type UserQuery { #定义查询的类型
     *   user : User #指定对象
     * }
     *
     * @return
     */
    public static GraphQLFieldDefinition createUserDefinition(GraphQLObjectType
                                                                      userType) {
        return newFieldDefinition()
                .name("User")
                .type(userType)
                //静态数据
                .dataFetcher(new StaticDataFetcher(new User(1L, "张三", 20)))
                .build();
    }
    public static GraphQLFieldDefinition createCardTypeDefinition(GraphQLObjectType cardType) {
        return newFieldDefinition()
                .name("Card")
                .type(cardType)
                .dataFetcher(new StaticDataFetcher(new Card("张三的卡", 20L)))
                .build();
    }

    /**
     * 定义User对象类型
     *
     * type User { #定义对象
     *   id:Long! # !表示该属性是非空项
     *   name:String
     *   age:Int
     * }
     *
     *
     * @return
     */
    public static GraphQLObjectType createUserObjectType(){
        return newObject()
                .name("User")
                .field(newFieldDefinition().name("id").type(GraphQLLong))
                .field(newFieldDefinition().name("name").type(GraphQLString))
                .field(newFieldDefinition().name("age").type(GraphQLInt))
                .build();
    }


    public static GraphQLObjectType createCardObjectType(){
        return newObject()
                .name("Card")
                .field(newFieldDefinition().name("userId").type(GraphQLLong))
                .field(newFieldDefinition().name("cardNumber").type(GraphQLString))
                .build();
    }
}
